<template>
  <div class="goodsOrder">
    <OrderHeader></OrderHeader>
    <div class="goodsOrder-main">
      <!-- 通知栏 -->
      <div class="noticeBar">
        <span>温馨提示：下载蘑菇街APP，及时跟进订单信息哟！</span>
      </div>
      <!-- 地址栏区域 -->
      <div class="goodsOrder-address">
        <div class="address-left">
          <i class="iconfont icon-dingwei"></i>
          <!-- <span>请填写收货地址</span> -->
          <div>
            <p class="user-info">张三 13288888888</p>
            <p class="address-info">广东省广州市天河区天河公园</p>
          </div>
        </div>
        <div class="address-right">
          <i class="iconfont icon-youjiantou"></i>
        </div>
      </div>
      <!-- 中间商品订单 -->
      <div class="goodsOrder-goods">
        <div v-for="(goods, i) in orderList" :key="i" class="goods-item">
          <CartGoodsItem :goods="goods"></CartGoodsItem>
        </div>
      </div>
    </div>
    <!-- 底部信息区域 -->
    <div class="goodsOrder-footer-bar showfff">
      <div class="footer-warp">
        <div class="paybar-detail">
          <div class="pricveNum">
            总价：
            <span class="price-icon">￥</span>
            <span class="price-sum">{{ totals.toFixed(2) }}</span>
          </div>
          <div class="totalNum">共{{ totalNum }}件商品</div>
        </div>
        <span class="paybar-btn" @click="paybarBtn">提交订单</span>
      </div>
    </div>
  </div>
</template>

<script>
import CartGoodsItem from "@/components/CartGoodsItem.vue";
import { mapGetters, mapMutations } from "vuex";
import OrderHeader from "@/components/OrderHeader.vue";
export default {
  components: {
    CartGoodsItem,
    OrderHeader,
  },
  data() {
    return {
      goodsObj: this.$route.query.goodsObj,
    };
  },
  computed: {
    ...mapGetters("m_cart", ["total", "checkedCount"]),
    totalNum() {
      return this.orderList.reduce((pre, item) => (pre += item.goods_stock), 0);
    },
    totals() {
      if (this.goodsObj) {
        return this.goodsObj.reduce(
          (pre, i) => (pre += i.goods_price * i.goods_stock),
          0
        );
      } else {
        return this.total;
      }
    },
    orderList() {
      if (this.goodsObj) {
        return this.goodsObj;
      } else {
        return this.checkedCount;
      }
    },
  },
  methods: {
    ...mapMutations("m_cart", ["updateOrderList", "delGoodsChecked"]),
    paybarBtn() {
      this.$toast({
        message: "订单成功生成",
        position: "bottom",
      });
      setTimeout(() => {
        this.$dialog
          .confirm({
            title: "支付提示",
          })
          .then(() => {
            // on confirm
            // 2.待发货
            this.updateOrderList({
              type: 2,
              data: this.goodsObj,
            });

            if (!this.goodsObj) {
              this.delGoodsChecked();
            }
            this.$router.replace({
              path: "/goodsorderlist",
            });
          })
          .catch(() => {
            // on cancel
            // 1.待付款
            this.updateOrderList({ type: 1, data: this.goodsObj });
            if (!this.goodsObj) {
              this.delGoodsChecked();
            }
            this.$router.replace("/goodsorderlist");
          });
      }, 500);
    },
  },

  mounted() {
    document
      .querySelector("body")
      .setAttribute("style", " background-color: #f6f6f6");
  },
  beforeDestroy() {
    document.body.removeAttribute("style");
  },
};
</script>

<style lang="scss" scoped>
body {
  background-color: #f6f6f6;
}
.showfff {
  background: #fff;
}
.goodsOrder-main {
  .noticeBar {
    background: #ffe6e8;
    padding: 7.5px 15px;
    color: #ff5375;
    white-space: nowrap;
    font-size: 14px;
    span {
      font-size: 12px;
    }
  }
  .goodsOrder-address {
    height: 45px;
    // width: 100%;
    background-color: skyblue;
    position: relative;
    min-height: 50px;
    line-height: 16.5px;
    padding: 15px;
    margin: 0 0 9px;
    color: #333;
    background: #fff url(@/assets/image/gelidai.png) repeat-x;
    background-size: 6%;
    display: flex;
    box-sizing: border-box;
    align-items: center;
    .address-left {
      flex: 1;
      display: flex;
      align-items: center;
      font-size: 14px;
      margin-top: 10px;
      .user-info {
        font-weight: bold;
      }
      .address-info {
        font-size: 12px;
      }
      .icon-dingwei {
        font-size: 25px;
        margin-right: 5px;
      }
    }
    .address-right {
      margin-top: 10px;
      text-align: right;
      .icon-youjiantou {
        line-height: 22px;
        font-size: 22px;
        color: #5e5e5e;
      }
    }
  }
  .goodsOrder-goods {
    .goods-item {
      margin-top: 10px;
      padding: 0 10px;
    }
  }
}
.goodsOrder-footer-bar {
  position: fixed;
  bottom: 0;
  width: 100%;
  .footer-warp {
    height: 50px;
    border-top: 1px solid #e5e5e5;
    padding-left: 15px;
    background: #fff;
    display: flex;
    align-items: center;
  }
  .paybar-detail {
    flex: 1;
    display: flex;
    align-items: center;
    .pricveNum {
      flex: 1;
      line-height: 20px;
      font-size: 14px;
      color: #333;
      .price-icon {
        color: #f46;
        font-size: 12px;
      }
      .price-sum {
        font-weight: bold;
        color: #f46;
        font-size: 18px;
      }
    }
    .totalNum {
      font-size: 12px;
      color: #999999;
      margin-right: 10px;
    }
  }
  .paybar-btn {
    display: block;
    width: 100px;
    height: 50px;
    line-height: 50px;
    text-align: center;
    background-image: -webkit-gradient(
      linear,
      left top,
      right top,
      from(#f46),
      to(#ff468f)
    );
    background-image: -webkit-linear-gradient(left, #f46, #ff468f);
    background-image: linear-gradient(90deg, #f46, #ff468f);
    color: #fff;
    font-size: 14px;
  }
}
</style>